import * as config from '../config';

export function date(str, format) {
  var date = new Date(str);
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  // 'yyyy-MM-dd hh:mm:ss'
  let output = '';
  output = format.replace('yyyy', year)
    .replace('MM', to2(month))
    .replace('dd', to2(day))
    .replace('hh', to2(hour))
    .replace('mm', to2(minutes))
    .replace('ss', to2(seconds));
  return output;
}

function to2(ipt) {
  return ipt > 9 ? ipt : ('' + 0 + ipt);
}

export function getCHNDay(date) {
  let day = date.getDay();
  return {
    0: '周日',
    1: '周一',
    2: '周二',
    3: '周三',
    4: '周四',
    5: '周五',
    6: '周六'
  }[day];
}

export function getCHNMonth(date) {
  var monthArr = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
  var res = monthArr[new Date(date).getMonth()];
  return res;
}

/**
 * [七牛图片处理过滤器]
 * @param  {[type]} url       [七牛图片原始地址]
 * @param  {[type]} mode      [模式] 1是要裁剪的 0是不要裁剪的
 * @param  {[type]} width     [宽度]
 * @param  {[type]} height    [高度]
 * @param  {[type]} quality   [质量]
 * @param  {[type]} interlace [渐进]
 * @return {[type]}           [新地址]
 * imageMogr2/auto-orient
 /thumbnail/<imageSizeGeometry>
 /strip
 /gravity/<gravityType>
 /crop/<imageSizeAndOffsetGeometry>
 /rotate/<rotateDegree>
 /format/<destinationImageFormat>
 /blur/<radius>x<sigma>
 /interlace/<Interlace>
 /quality/<quality>
 /size-limit/<sizeLimit>
 * @example  <img src="../static/images/1x1.png" v-lazy="item.imgUrl | qnParser '1' '513' '513'" />
 * @API地址 http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
 */
export function qnParser(url, mode = 0, width = '', height = '', quality, interlace = 1) {
  if (url && (url.indexOf('imageView2') > -1 || url.indexOf('imageMogr2') > -1)) {
    return url;
  }
  let targetUrl;
  let thumbnail = '';
  let gravityCrop = '';
  let q = '';
  // 如果有设置宽高则取缩略图
  if (width || height) {
    thumbnail = `/thumbnail/${width}x${height}`;
  }
  // 如果宽高都有设置，则截取
  if (width && height && mode === 1) {
    gravityCrop = `/gravity/Center/crop/${width}x${height}`;
  }

  if (quality) {
    q = `/quality/${quality}`;
  } else {
    let qValue;
    qValue = {
      '2g': 50,
      '3g': 60,
      '4g': 80,
      'wifi': 90
    }[window.support.networkType] || 90;
    q = `/quality/${qValue}`;
  }

  targetUrl = `${url}?imageMogr2/auto-orient/strip${gravityCrop}${thumbnail}${q}/interlace/1`;
  // 判断是否需要WEBP格式
  if (window.support.isWEBP) {
    targetUrl += '/format/webp';
  }
  return targetUrl;
}

/**
 * 判断是否支持WEBP 然后给出WEBP格式图片
 * @param url
 */
export function webP(url) {
  if (window.support.isWEBP) {
    return url + '.webp';
  } else {
    return url;
  }
}

/**
 * 获取当前router页面下的分享配置
 *
 */
export function shareMap(routerName) {
  function getShareData(title, desc, link = window.location.href, imgUrl = config.config.shareInfo.logo) {
    // TODO 这里需要为LINK 添加 渠道
    if (link.indexOf('?') === -1) {
      link += '?';
    }
    if (window.sessionStorage['channel']) {
      link += '&channel=' + window.sessionStorage['channel'];
    }
    // if (window.sessionStorage['from']) {
    //  link += '&from=' + window.sessionStorage['from'];
    // }
    // window.sessionStorage['from'] = $router._currentRoute.query.from || window.sessionStorage['from'] || '';)
    return {
      'title': title, // 分享标题
      'desc': desc, // 分享的描述
      'link': link, // 分享链接
      'imgUrl': imgUrl // 分享的图标
    };
  }

  var maps = {
    index: getShareData(config.config.shareInfo.text, config.config.shareInfo.text),
    search: getShareData('买道商城：搜索', '买道商城：搜索'),
    listhotSale: getShareData('买道商城：热销', '买道商城：热销'),
    listSecKill: getShareData('买道商城：活动', '买道商城：活动'),
    listNew: getShareData('买道商城：上新', '买道商城：上新'),
    listDiscount: getShareData('买道商城：折扣', '买道商城：折扣'),
    listActivity: getShareData('买道商城：活动', '买道商城：活动'),
    shop: getShareData('买道商城：店铺', '买道商城：店铺'),
    activity: getShareData('买道商城：活动', '买道商城：活动'),
    defaults: getShareData('买道商城', '买道商城')
  };
  return maps[routerName];
}

export default {
  setFilter (_vue) {
    _vue.filter('getCHNDay', getCHNDay);
    _vue.filter('getCHNMonth', getCHNMonth);
    _vue.filter('qnParser', qnParser);
    _vue.filter('date', date);
  }
};
